package com.virtualmall.message;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import com.virtualmall.utils.ISOUtils;

//import org.apache.log4j.Logger;

public class SocketClient {
    
    //private static Logger logger = Logger.getLogger(SocketClient.class);
    
    private String host;
    
    private int port;

    private static DataOutputStream outs;
    
    public void init(){
        //logger.info("socket cilent start method begin...");
        
//        try {
//            Socket client = new Socket(host,port);  
//            OutputStream out = client.getOutputStream();  
//            outs = new DataOutputStream(out);  while(true) {  
//                processMesage(client);  
//            }
//        } catch (IOException e) {
//            //logger.error(e);
//        } 
        
        //logger.info("socket cilent start method end...");
        
        try {
            SocketClient.sendMsg("zhangwenlong","content");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void sendMsg(String userName,String content) throws IOException {

        byte[] toUserName = ISOUtils.getString2Bytes(userName);
        byte[] talkMsg = ISOUtils.getString2Bytes(content);
        byte[] msg = new byte[10 + toUserName.length + talkMsg.length];
        
        int msgI = 2;
        byte[] msgFlags = ISOUtils.short1Bytes((short)0);
        for (int i = 0; i < msgFlags.length; i ++) {
            msg[msgI + i] = msgFlags[i];
        }
        msgI += msgFlags.length;

        byte[] type = ISOUtils.short1Bytes((short)0);
        for (int i = 0; i < type.length; i ++) {
            msg[msgI + i] = type[i];
        }
        msgI += type.length;

        byte[] msgID = ISOUtils.short2Bytes((short)31003);
        for (int i = 0; i < msgID.length; i ++) {
            msg[msgI + i] = msgID[i];
        }
        msgI += msgID.length;

        for (int i = 0; i < toUserName.length; i ++) {
            msg[msgI + i] = toUserName[i];
        }
        msgI += toUserName.length;

        byte[] userNameLen = ISOUtils.short2Bytes((short)(toUserName.length*8));
        for (int i = 0; i < userNameLen.length; i ++) {
            msg[msgI + i] = userNameLen[i];
        }
        msgI += userNameLen.length;

        for (int i = 0; i < talkMsg.length; i ++) {
            msg[msgI + i] = talkMsg[i];
        }
        msgI += talkMsg.length;

        byte[] talkLen = ISOUtils.short2Bytes((short)(talkMsg.length*8));
        for (int i = 0; i < talkLen.length; i ++) {
            msg[msgI + i] = talkLen[i];
        }
        msgI += talkLen.length;

        byte[] msgLen = ISOUtils.short2Bytes((short) ((msgFlags.length
                + type.length + msgID.length + toUserName.length
                + userNameLen.length + talkMsg.length + talkLen.length) * 8));
        for (int i = 0; i < msgLen.length; i ++) {
            msg[0 + i] = msgLen[i];
        }
        
//        int totalLen = 1 + 4 + bytes.length;
//        outs.writeByte(1);
//        outs.writeInt(totalLen);
//        outs.write(msg);
//        outs.flush();
    }
    
    private void processMesage(Socket client) throws IOException {  
        InputStream ins = client.getInputStream();
        DataInputStream dins = new DataInputStream(ins);
        //服务端解包过程
        while(true) {
            int totalLen = dins.readInt();
            byte flag = dins.readByte();
            System.out.println("接收消息类型"+flag);

            byte[] data = new byte[totalLen - 4 - 1];
            dins.readFully(data);
            String msg = new String(data);
            System.out.println("发来的内容是:"+msg);
        }
    }
    
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

}
